package com.chaos.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.List;

public class CodeGenerator {

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        CodeHelper.moduleName = CodeHelper.scanner("模块名");
        CodeHelper.projectName = CodeHelper.moduleName.substring(0, 1).toUpperCase() + CodeHelper.moduleName.substring(1);

        //全局配置
        CodeHelper.initGlobalConfig(mpg);
        //包配置
        CodeHelper.initPackageConfig(mpg);
        //数据库配置
        CodeHelper.initDataSource(mpg);

        CodeHelper.initInjectionConfig(mpg);

        CodeHelper.initTemplateConfig(mpg);
        CodeHelper.initStrategyConfig(mpg);


        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
        generatedTableInfoCheck(mpg);
        System.out.println("Done");
    }


    private static void generatedTableInfoCheck(AutoGenerator mpg) {
        List<TableInfo> tableInfoList = mpg.getConfig().getTableInfoList();
        if (tableInfoList.isEmpty()) {
            System.out.println("!!!!!!!!!  本次生成未查询到任何表，请确认数据库配置或者输入的表名称。 !!!!!!!!!");
        } else {
            System.out.println("=================== 生成表信息展示 ===================");
            for (TableInfo tableInfo : tableInfoList) {
                String name = tableInfo.getEntityName();
                String comment = tableInfo.getComment();
                System.out.println("表：" + tableInfo.getName() + " 已生成成功！");
                String route = "  {\n" +
                        "    path: '/" + name + "',\n" +
                        "    icon: 'table',\n" +
                        "    name: '" + name + "',\n" +
                        "    component: './" + name + "',\n" +
                        "  },";
                System.out.println("请添加 saas 端路由：\n" + route);

                String locales = "'menu." + name + "': '" + comment + "',";
                System.out.println("请添加 saas 端locales：\n" + locales);


            }
            System.out.println("=================== 总生成表数：" + tableInfoList.size() + " ===================");
        }
    }


}
